package com.hx.er.common.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.bean.base.RestraurantBean;
import com.hx.er.service.business.IRestraurantService;

public class ErRestraurant {
	private static Map<Long, RestraurantBean> restMap;
	private static Map<String, RestraurantBean> restServerMap;

	@Autowired
	private IRestraurantService restraurantService;

	public void load() {
		List<RestraurantBean> restList = new ArrayList<RestraurantBean>();

		if (restMap == null) {
			restMap = new HashMap<Long, RestraurantBean>();
		}
		if (restServerMap == null) {
			restServerMap = new HashMap<String, RestraurantBean>();
		}

		// 检索条件，只查询有效的餐馆
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("statusId", 1);
		restList = this.restraurantService.getRestList(conditions);
		for (RestraurantBean rest : restList) {
			restMap.put(rest.getRestId(), rest);
			restServerMap.put(rest.getServerName(), rest);
			System.out.println(rest.getRestName() + ":" + rest.getServerName());
		}
	}

	public void reload() {
		this.load();
	}

	public synchronized static void removeRest(long restId) {
		// 获取bean
		RestraurantBean restBean = restMap.get(restId);
		
		// 移除餐厅
		restMap.remove(restId);
		restServerMap.remove(restBean.getServerName());
	}

	public synchronized static void addRest(RestraurantBean restBean) {
		// 将餐厅加入系统缓存
		restMap.put(restBean.getRestId(), restBean);
		restServerMap.put(restBean.getServerName(), restBean);
	}

	public static Map<Long, RestraurantBean> getRestMap() {
		return restMap;
	}

	public static RestraurantBean getRest(long restId) {
		return restMap.get(restId);
	}

	public static RestraurantBean getRestByServerName(String serverName) {
		return restServerMap.get(serverName);
	}

	public static int getCount() {
		return restMap.size();
	}
}
